package com.esri.core.geodatabase;

import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.internal.tasks.e;
import com.esri.core.internal.tasks.f.c;
import com.esri.core.internal.util.LocalGDBUtil;
import com.esri.core.internal.util.d;
import com.esri.core.internal.util.f;
import com.esri.core.map.CallbackListener;
import com.esri.core.map.Feature;
import com.esri.core.map.FeatureResult;
import com.esri.core.map.Field;
import com.esri.core.table.FeatureTable;
import com.esri.core.table.TableException;
import com.esri.core.tasks.query.QueryParameters;
import java.io.FileNotFoundException;
import java.io.StringWriter;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.MappingJsonFactory;

/* loaded from: classes.dex */
public final class ShapefileFeatureTable extends FeatureTable {
    private static final String a = "Shapefile editing is not supported";
    private final Geodatabase b;
    private List<Field> c;
    private SpatialReference d;

    private String a() {
        return null;
    }

    @Override // com.esri.core.table.FeatureTable
    public String getCopyright() {
        return null;
    }

    @Override // com.esri.core.table.FeatureTable
    public boolean hasGeometry() {
        return true;
    }

    @Override // com.esri.core.table.FeatureTable
    public boolean isEditable() {
        return false;
    }

    native long[] nativeAddFeature(long j, String str);

    native long[] nativeCheckIDs(long j, long[] jArr);

    native void nativeDeleteRows(long j, long[] jArr);

    native String nativeGetAttributes(long j, long j2);

    native double[] nativeGetExtent(long j);

    native String nativeGetFields(long j);

    native byte[] nativeGetGeometry(long j, long j2);

    native String nativeGetGeometryType(long j);

    native long nativeGetRowCount(long j);

    native String nativeGetRowIdFieldName(long j);

    native int nativeGetSpatialReference(long j);

    native void nativeUpdateAttributes(long j, long j2, String str);

    native void nativeUpdateGeometry(long j, long j2, byte[] bArr);

    static {
        f.a().initialize();
    }

    public ShapefileFeatureTable(String str) throws FileNotFoundException {
        if (str == null) {
            throw new NullPointerException("Path cannot be null.");
        }
        this.b = Geodatabase.a(str);
        this.d = SpatialReference.create(nativeGetSpatialReference(getHandle()));
    }

    @Override // com.esri.core.table.FeatureTable
    public long addFeature(Feature feature) throws TableException {
        throw new UnsupportedOperationException(a);
    }

    @Override // com.esri.core.table.FeatureTable
    public long[] addFeatures(List<Feature> list) throws TableException {
        throw new UnsupportedOperationException(a);
    }

    @Override // com.esri.core.table.FeatureTable
    public void deleteFeature(long j) throws TableException {
        throw new UnsupportedOperationException(a);
    }

    @Override // com.esri.core.table.FeatureTable
    public void deleteFeatures(long[] jArr) throws TableException {
        throw new UnsupportedOperationException(a);
    }

    @Override // com.esri.core.table.FeatureTable
    public Feature getFeature(long j) throws TableException {
        return getFeature(j, null);
    }

    public Feature getFeature(long j, SpatialReference spatialReference) throws TableException {
        Geometry geometry;
        if (getHandle() == 0) {
            return null;
        }
        byte[] nativeGetGeometry = nativeGetGeometry(getHandle(), j);
        if (nativeGetGeometry != null) {
            Geometry geometryFromEsriShape = GeometryEngine.geometryFromEsriShape(nativeGetGeometry, Geometry.Type.UNKNOWN);
            if (spatialReference != null) {
                geometryFromEsriShape = GeometryEngine.project(geometryFromEsriShape, getSpatialReference(), spatialReference);
            }
            geometry = geometryFromEsriShape;
        } else {
            geometry = null;
        }
        Map<String, Object> a2 = d.a(nativeGetAttributes(getHandle(), j));
        if (a2.isEmpty() && geometry == null) {
            return null;
        }
        return new ShapefileFeature(j, a2, geometry, this);
    }

    @Override // com.esri.core.table.FeatureTable
    public FeatureResult getFeatures(long[] jArr) {
        if (getHandle() == 0) {
            return null;
        }
        return new b(this, jArr, null, a(), getObjectIdField(), getFields());
    }

    @Override // com.esri.core.table.FeatureTable
    public long getNumberOfFeatures() {
        if (getHandle() == 0) {
            return -1L;
        }
        return nativeGetRowCount(getHandle());
    }

    @Override // com.esri.core.table.FeatureTable
    public Field getField(String str) {
        if (getHandle() == 0) {
            return null;
        }
        for (Field field : getFields()) {
            if (field.getName().equalsIgnoreCase(str)) {
                return field;
            }
        }
        return null;
    }

    @Override // com.esri.core.table.FeatureTable
    public List<Field> getFields() {
        if (getHandle() == 0) {
            return Collections.emptyList();
        }
        List<Field> list = this.c;
        if (list != null) {
            return list;
        }
        try {
            String nativeGetFields = nativeGetFields(getHandle());
            if (nativeGetFields != null) {
                this.c = Field.fromJson(nativeGetFields);
            }
            return this.c;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override // com.esri.core.table.FeatureTable
    public String getTableName() {
        return this.b.b().get(0);
    }

    @Override // com.esri.core.table.FeatureTable
    public Future<FeatureResult> queryFeatures(final QueryParameters queryParameters, final CallbackListener<FeatureResult> callbackListener) {
        return e.b.submit(new Callable<FeatureResult>() { // from class: com.esri.core.geodatabase.ShapefileFeatureTable.1
            @Override // java.util.concurrent.Callable
            /* renamed from: a */
            public FeatureResult call() {
                return (queryParameters.getOutStatistics() == null || queryParameters.getOutStatistics().length <= 0) ? ShapefileFeatureTable.this.b(queryParameters, callbackListener) : ShapefileFeatureTable.this.a(queryParameters, callbackListener);
            }
        });
    }

    @Override // com.esri.core.table.FeatureTable
    public Future<long[]> queryIds(final QueryParameters queryParameters, final CallbackListener<long[]> callbackListener) {
        return e.b.submit(new Callable<long[]>() { // from class: com.esri.core.geodatabase.ShapefileFeatureTable.2
            /* JADX WARN: Code restructure failed: missing block: B:14:0x0026, code lost:
                if (r1 != null) goto L6;
             */
            /* JADX WARN: Code restructure failed: missing block: B:16:0x0029, code lost:
                return r0;
             */
            @Override // java.util.concurrent.Callable
            /* renamed from: a */
            /*
                Code decompiled incorrectly, please refer to instructions dump.
                To view partially-correct code enable 'Show inconsistent code' option in preferences
            */
            public long[] call() {
                /*
                    r5 = this;
                    r0 = 0
                    com.esri.core.geodatabase.ShapefileFeatureTable r1 = com.esri.core.geodatabase.ShapefileFeatureTable.this     // Catch: java.lang.Throwable -> L1a java.lang.Exception -> L1c
                    long r1 = r1.getHandle()     // Catch: java.lang.Throwable -> L1a java.lang.Exception -> L1c
                    r3 = 0
                    com.esri.core.tasks.query.QueryParameters r4 = r2     // Catch: java.lang.Throwable -> L1a java.lang.Exception -> L1c
                    java.lang.String r4 = r4.toJSON()     // Catch: java.lang.Throwable -> L1a java.lang.Exception -> L1c
                    long[] r0 = com.esri.core.internal.util.LocalGDBUtil.nativeQueryIdsTableIdx(r1, r3, r4)     // Catch: java.lang.Throwable -> L1a java.lang.Exception -> L1c
                    com.esri.core.map.CallbackListener r1 = r3
                    if (r1 == 0) goto L29
                L16:
                    r1.onCallback(r0)
                    goto L29
                L1a:
                    r1 = move-exception
                    goto L30
                L1c:
                    r1 = move-exception
                    com.esri.core.map.CallbackListener r2 = r3     // Catch: java.lang.Throwable -> L1a
                    if (r2 == 0) goto L2a
                    r2.onError(r1)     // Catch: java.lang.Throwable -> L1a
                    com.esri.core.map.CallbackListener r1 = r3
                    if (r1 == 0) goto L29
                    goto L16
                L29:
                    return r0
                L2a:
                    java.lang.RuntimeException r2 = new java.lang.RuntimeException     // Catch: java.lang.Throwable -> L1a
                    r2.<init>(r1)     // Catch: java.lang.Throwable -> L1a
                    throw r2     // Catch: java.lang.Throwable -> L1a
                L30:
                    com.esri.core.map.CallbackListener r2 = r3
                    if (r2 == 0) goto L37
                    r2.onCallback(r0)
                L37:
                    goto L39
                L38:
                    throw r1
                L39:
                    goto L38
                */
                throw new UnsupportedOperationException("Method not decompiled: com.esri.core.geodatabase.ShapefileFeatureTable.AnonymousClass2.call():long[]");
            }
        });
    }

    @Override // com.esri.core.table.FeatureTable
    public void updateFeature(long j, Feature feature) throws TableException {
        throw new UnsupportedOperationException(a);
    }

    @Override // com.esri.core.table.FeatureTable
    public void updateFeatures(long[] jArr, List<Feature> list) throws TableException {
        throw new UnsupportedOperationException(a);
    }

    @Override // com.esri.core.table.FeatureTable
    public Envelope getExtent() {
        double[] nativeGetExtent;
        if (getHandle() == 0 || (nativeGetExtent = nativeGetExtent(getHandle())) == null || nativeGetExtent.length != 4) {
            return null;
        }
        return new Envelope(nativeGetExtent[0], nativeGetExtent[1], nativeGetExtent[2], nativeGetExtent[3]);
    }

    @Override // com.esri.core.table.FeatureTable
    public SpatialReference getSpatialReference() {
        return this.d;
    }

    public void dispose() {
        Geodatabase geodatabase = this.b;
        if (geodatabase != null) {
            geodatabase.dispose();
        }
    }

    protected void finalize() throws Throwable {
        dispose();
    }

    public Geometry.Type getGeometryType() {
        return getHandle() == 0 ? Geometry.Type.UNKNOWN : d.b(nativeGetGeometryType(getHandle()));
    }

    public String getObjectIdField() {
        if (getHandle() == 0) {
            return null;
        }
        return nativeGetRowIdFieldName(getHandle());
    }

    private String a(Feature[] featureArr, boolean z, boolean z2) throws Exception {
        StringBuilder sb = new StringBuilder();
        if (z2) {
            sb.append("{ \"features\": ");
        }
        sb.append('[');
        boolean z3 = true;
        for (Feature feature : featureArr) {
            if (feature != null) {
                if (z3) {
                    String a2 = a(feature, getSpatialReference(), z);
                    if (a2 != null) {
                        sb.append(a2);
                        z3 = false;
                    }
                } else {
                    String a3 = a(feature, getSpatialReference(), z);
                    if (a3 != null) {
                        sb.append(',');
                        sb.append(a3);
                    }
                }
            }
        }
        sb.append(']');
        if (z2) {
            sb.append("}");
        }
        return sb.toString();
    }

    private static String a(Feature feature, SpatialReference spatialReference, boolean z) throws Exception {
        StringWriter stringWriter = new StringWriter();
        JsonGenerator createJsonGenerator = new MappingJsonFactory().createJsonGenerator(stringWriter);
        createJsonGenerator.writeStartObject();
        if (!feature.getAttributes().isEmpty()) {
            Map<String, Object> attributes = feature.getAttributes();
            createJsonGenerator.writeFieldName("attributes");
            createJsonGenerator.writeRawValue(d.a(attributes));
        }
        if (feature.getGeometry() != null && z) {
            createJsonGenerator.writeFieldName("geometry");
            createJsonGenerator.writeRawValue(GeometryEngine.geometryToJson(spatialReference, feature.getGeometry()));
        }
        createJsonGenerator.writeEndObject();
        createJsonGenerator.close();
        return stringWriter.getBuffer().toString();
    }

    public long getHandle() {
        Geodatabase geodatabase = this.b;
        if (geodatabase != null) {
            return geodatabase.getHandle();
        }
        return 0L;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public FeatureResult a(QueryParameters queryParameters, CallbackListener<FeatureResult> callbackListener) {
        FeatureResult featureResult = null;
        try {
            try {
                featureResult = c.a(LocalGDBUtil.nativeQueryAggregateTableIdx(getHandle(), 0, queryParameters.toJSON()));
            } catch (Exception e) {
                if (callbackListener != null) {
                    callbackListener.onError(e);
                    if (callbackListener != null) {
                    }
                    return featureResult;
                }
                throw new RuntimeException(e);
            }
        } finally {
            if (callbackListener != null) {
                callbackListener.onCallback(null);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public FeatureResult b(QueryParameters queryParameters, CallbackListener<FeatureResult> callbackListener) {
        try {
            try {
                b bVar = new b(this, LocalGDBUtil.nativeQueryIdsTableIdx(getHandle(), 0, queryParameters.toJSON()), queryParameters.getOutSpatialReference(), "", getObjectIdField(), null);
                if (callbackListener != null) {
                    callbackListener.onCallback(bVar);
                }
                return bVar;
            } catch (Exception e) {
                if (callbackListener != null) {
                    callbackListener.onError(e);
                    if (callbackListener != null) {
                        callbackListener.onCallback(null);
                        return null;
                    }
                    return null;
                }
                throw new RuntimeException(e);
            }
        } catch (Throwable th) {
            if (callbackListener != null) {
                callbackListener.onCallback(null);
            }
            throw th;
        }
    }

    /* loaded from: classes.dex */
    class a implements Iterator<Object> {
        AtomicInteger a = new AtomicInteger(0);
        ShapefileFeatureTable b;
        SpatialReference c;
        private long[] e;

        public a(ShapefileFeatureTable shapefileFeatureTable, long[] jArr, SpatialReference spatialReference) {
            if (shapefileFeatureTable == null) {
                throw new RuntimeException("table cannot be null");
            }
            if (jArr == null) {
                throw new RuntimeException("oids cannot be null");
            }
            this.e = ShapefileFeatureTable.this.nativeCheckIDs(ShapefileFeatureTable.this.getHandle(), jArr);
            this.b = shapefileFeatureTable;
            this.c = spatialReference;
        }

        public int a() {
            return this.e.length;
        }

        @Override // java.util.Iterator
        public boolean hasNext() {
            return this.a.get() < a();
        }

        @Override // java.util.Iterator
        /* renamed from: b */
        public Feature next() {
            try {
                Feature feature = this.b.getFeature(this.e[this.a.get()], this.c);
                this.a.incrementAndGet();
                return feature;
            } catch (TableException e) {
                throw new RuntimeException(e);
            } catch (ArrayIndexOutOfBoundsException unused) {
                throw new NoSuchElementException();
            }
        }

        @Override // java.util.Iterator
        public void remove() {
            throw new UnsupportedOperationException(ShapefileFeatureTable.a);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public class b extends FeatureResult {
        long[] a;
        SpatialReference b;
        ShapefileFeatureTable c;

        b(ShapefileFeatureTable shapefileFeatureTable, long[] jArr, SpatialReference spatialReference, String str, String str2, List<Field> list) {
            this.a = null;
            this.b = null;
            this.c = null;
            this.a = jArr == null ? new long[0] : jArr;
            this.b = spatialReference;
            this.c = shapefileFeatureTable;
            setDisplayFieldName(str);
            setObjectIdFieldName(str2);
            setFields(list);
        }

        @Override // com.esri.core.map.FeatureResult, java.lang.Iterable
        public Iterator<Object> iterator() {
            return new a(this.c, this.a, this.b);
        }

        @Override // com.esri.core.map.FeatureResult
        public long featureCount() {
            return this.a.length;
        }
    }
}
